<?php

class Transaction extends CI_Model{

	function __construct()
    	{
        	parent::__construct();
    	}
	
	function store_userprofile($fName, $mName, $lName, $nName, $email, $msisdn, $otherCont, $birthdate, $street, $area, $town, $province, $postalCode, $gadgetId, $promoId)
	{
		$this->load->database();
		date_default_timezone_set("Asia/Manila");
		$current_timestamp = date("Y-m-d H:i:s", time());
		
		$data = array(
			'fName' => $fName,
			'mName' => $mName,
			'lName' => $lName,
			'nName' => $nName,
			'email' => $email,
			'msisdn' => $msisdn,
			'otherCont' => $otherCont,
			'birthdate' => $birthdate,
			'street' => $street,
			'area' => $area,
			'town' => $town,
			'province' => $province,
			'postalCode' => $postalCode,
			'gadgetId' => $gadgetId,
			'promoId' => $promoId,
			'commitmentPeriod' => $current_timestamp
		);

		try
		{
			$this->db->insert('Maitai_Subscriber', $data);
			$this->db->close();
		}
		catch(Exception $e)
		{
			return FALSE;
		}
	}
	
	function saveUserProfileToDB($fName, $mName, $lName, $nName, $email, $msisdn, $otherCont, $birthdate, $street, $area, $town, $province, $postalCode, $gadgetId, $promoId)
	{
		$this->load->database();
		date_default_timezone_set("Asia/Manila");
		$current_timestamp = date("Y-m-d H:i:s", time());
		
		$data = array(
			'fName' => $fName,
			'mName' => $mName,
			'lName' => $lName,
			'nName' => $nName,
			'email' => $email,
			'msisdn' => $msisdn,
			'otherCont' => $otherCont,
			'birthdate' => $birthdate,
			'street' => $street,
			'area' => $area,
			'town' => $town,
			'province' => $province,
			'postalCode' => $postalCode,
			'gadgetId' => $gadgetId,
			'promoId' => $promoId,
			'commitmentPeriod' => $current_timestamp
		);
		
		$tbl_subscriber = $this->config->item('table_subscriber');
		$query = $this->db->get_where($tbl_subscriber, array('msisdn' => $msisdn));
		
		// IS MERON NA IN TABLE?
		if($q = $query->result_array()) {
			// NUMBER ALREADY IN DB
			$this->db->update($tbl_subscriber, $data, array('msisdn' => $msisdn));
			return TRUE;
		} else {
			// INSERT NEW DATA
			$this->db->insert($tbl_subscriber, $data);
			return TRUE;
		}
		return FALSE;
	}
	
	function saveStoreIdToDB($storeId)
	{
		$this->load->database();
		
		$data = array(
			'storeId' => $storeId
		);
		
		$tbl_subscriber = $this->config->item('table_subscriber');
		if(isset($_COOKIE['mobtel'])) $msisdn = $_COOKIE['mobtel'];
		$query = $this->db->get_where($tbl_subscriber, array('msisdn' => $msisdn));
		
		// IS MERON NA IN TABLE?
		if($q = $query->result_array()) {
			// NUMBER ALREADY IN DB
			$this->db->update($tbl_subscriber, $data, array('msisdn' => $msisdn));
			return TRUE;
		} else {
			return FALSE;
		}
		return FALSE;
	}
	
    	public function retrieveUserProfileToDB($mobtel)
	{   
		$this->load->database();
		$tbl_subscriber = $this->config->item('table_subscriber');
		$query = $this->db->get_where($tbl_subscriber, array('msisdn' => $mobtel));
		return $query->result_array(); 
	}

	public function updatePlanCode($mobtel, $plancode)
	{
		$this->load->database();
		if($mobtel!='' && $plancode!=''){
			$data = array(
				'$plancode' => plancode
			);
			$this->db->where('msisdn', $mobtel);
			$query = $this->db->update($this->config->item('table_subscriber'), $data); 
			return $query;
		}
		return false;
	}		
}
